package com.ll.beautymall.ui.home.zero.view;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.ll.beautymall.R;
import com.ll.beautymall.adapter.ZeroAdapter;
import com.ll.beautymall.api.UrlConfig;
import com.ll.beautymall.base.BaseActivity;
import com.ll.beautymall.bean.ModelContent;
import com.ll.beautymall.bean.ZeroData;
import com.ll.beautymall.bean.ZeroHeader;
import com.ll.beautymall.ui.home.zero.presenter.ZeroPresenter;
import com.squareup.picasso.Picasso;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

public class ZeroActivity extends BaseActivity implements IZeroView {

    @BindView(R.id.iv_category_detail_back)
    ImageView ivCategoryDetailBack;
    @BindView(R.id.tv_category_detail_title)
    TextView tvCategoryDetailTitle;
    @BindView(R.id.tb_zero)
    Toolbar tbZero;
    @BindView(R.id.xrv_zero)
    XRecyclerView xrvZero;
    private ZeroAdapter zeroAdapter;
    private TextView tv_title;

    @Override
    public int getLayoutId() {
        return R.layout.activity_zero;
    }

    @Override
    public void initView() {
        ZeroPresenter zeroPresenter = new ZeroPresenter(this);

        tbZero.inflateMenu(R.menu.zero_share);

        tbZero.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.menu_zero_share) {
                    //TODO:第三方分享
                }
                return false;
            }
        });

        ImageView iv_empty = new ImageView(this);
        iv_empty.setBackgroundResource(R.mipmap.img_begin_05);
        xrvZero.setEmptyView(iv_empty);


        XRecyclerView.LayoutManager manager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);
        xrvZero.setLayoutManager(manager);

        zeroAdapter = new ZeroAdapter(this);
        xrvZero.setAdapter(zeroAdapter);

        Intent intent = getIntent();
        ModelContent modelContent = (ModelContent) intent.getSerializableExtra("modelContent");
        HashMap<String, String> parameters = new HashMap<>();
        HashMap<String, String> headerParameter = new HashMap<>();

        String action = modelContent.getContent_data().getAction();
        String type = action.substring(action.lastIndexOf("/") + 1, action.indexOf("?"));
        parameters.put(UrlConfig.Key.TYPE, type);

        String result = action.substring(action.indexOf("?") + 1);
        String[] split = result.split("&");
        for (int i = 0; i < split.length; i++) {
            String[] para = split[i].split("=");
            if (para[0].equals("title")) {
                try {
                    String titleP = para[1].substring(1);
                    String title = URLDecoder.decode(titleP, "UTF-8");
                    tvCategoryDetailTitle.setText("0" + title);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            if (para[0].equals("activity_id")) {
                headerParameter.put(UrlConfig.Key.ACTIVITY_ID, para[1]);
                parameters.put(UrlConfig.Key.OBJECT_ID, para[1]);
            }
        }
        zeroPresenter.setZeroHeaderData(headerParameter);

        parameters.put(UrlConfig.Key.CURSOR, "0");
        parameters.put(UrlConfig.Key.COUNT, "40");

        zeroPresenter.setZeroData(parameters);
    }

    @OnClick(R.id.iv_category_detail_back)
    public void onClick() {
        finish();
    }

    @Override
    public void setZeroData(ZeroData zeroData) {
        zeroAdapter.setData(zeroData.getProducts());
    }

    @Override
    public void setZeroHeader(ZeroHeader zeroHeader) {
        View view = LayoutInflater.from(this).inflate(R.layout.zero_header, null, false);
        xrvZero.addHeaderView(view);
        ImageView iv_header = (ImageView) view.findViewById(R.id.iv_zero_header);
        tv_title = (TextView) view.findViewById(R.id.tv_zero_title);
        Picasso.with(this).load(zeroHeader.getPic_url()).into(iv_header);
        tv_title.setText(zeroHeader.getDescription());
    }

    @Override
    public void showErrorMsg(String errorMsg) {
        Toast.makeText(ZeroActivity.this, errorMsg, Toast.LENGTH_SHORT).show();
    }
}
